package com.uroad.lib.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.uroad.lib.R;
import com.uroad.lib.widget.photopicker.adapter.PhotoPagerAdapter;

import java.util.ArrayList;

public class ShowPicDialog extends Dialog {

    Context mContext;
    private ArrayList<String> paths;
    private ViewPager mViewPager;
    private TextView tvIndex;
    private PhotoPagerAdapter mPagerAdapter;
    private int currentItem = 0;

    public ShowPicDialog(Context context) {
        super(context, R.style.baseCustomDialog);
        mContext = context;
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.dialog_showpic, null);
        mViewPager = (ViewPager) view.findViewById(R.id.vp_photos);
        tvIndex = (TextView) view.findViewById(R.id.tvIndex);
        setCanceledOnTouchOutside(false);
        setContentView(view);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置全屏
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }

    public void setData(ArrayList<String> paths, int currentItem) {
        this.paths = paths;
        this.currentItem = currentItem;
        mPagerAdapter = new PhotoPagerAdapter(Glide.with(mContext), this.paths);
        mPagerAdapter.setOnImageViewClick(new PhotoPagerAdapter.OnImageViewClick() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setCurrentItem(currentItem);
        if (paths.size() == 1) {
            tvIndex.setVisibility(View.GONE);
        }
        tvIndex.setText((currentItem + 1) + "/" + paths.size());
        mViewPager.setOffscreenPageLimit(5);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tvIndex.setText((position + 1) + "/" + ShowPicDialog.this.paths.size());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void hideDialog() {
        this.dismiss();
    }
}
